// pages/me/me.js
const app = getApp();
const db = wx.cloud.database();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        fileinfo: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            userInfo: app.globalData.userInfo,
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    addnews: function() {
        wx.navigateTo({
          url: '../addnews/addnews',
        })
    },

    insert: function() {
        db.collection('user').add({

            data: {
                name: 'jack',
                age: 20
            },

            success: res => {
                console.log(res);
            },

            fail: err => {
                console.log(err);
            }

        })
    },

    update: function() {
        db.collection('user').doc('cd045e7561667f5e14d902d4065c8768').update({
            
            data: {
                age: 21
            }
            
        }).then(
            res => {
                console.log(res);
            }).catch(
            err => {
                console.log(err);
            }
        )
    },

    query: async function() {
        let query1 = await db.collection('news').doc('cd045e756166867514da35e117529100').get()
        let query2 = await db.collection('news').doc('14139e126166885e1298e6653d268f4a').get()
        console.log(query1.data);
        console.log(query2.data);
    },

    delete: async function() {
        let del = await db.collection('user').doc('cd045e7561667f5e14d902d4065c8768').remove()
        console.log(del);
    },

    uploadFile: async function() {
        wx.chooseMessageFile({
          count: 1,
          type: 'all',
          success: res => {
              console.log(res);
              wx.showLoading({
                title: '文件正在上传......',
              })
              let randomString = Math.floor(Math.random() * 1000000).toString();
              let fileName = res.tempFiles[0].name;
              let filepath = randomString + fileName;
              this.data.fileinfo.name = fileName;
              wx.cloud.uploadFile({
                  cloudPath: 'file/' + filepath,
                  filePath: res.tempFiles[0].path
              }).then(res => {
                  this.data.fileinfo.fileID = res.fileID;
                  let cpfilename = fileName;
                  let index1 = cpfilename.lastIndexOf('.') + 1;
                  let index2 = cpfilename.length;
                  let type = cpfilename.substring(index1, index2);
                  this.data.fileinfo.fileType = type;
                  console.log(this.data.fileinfo);
                  db.collection('files').add({
                      data: this.data.fileinfo
                  });
              });
              wx.hideLoading({
                success: (res) => {
                    wx.showToast({
                      title: '上传成功',
                    })
                },
              });
          }
        })
    },

    downloadFile: function() {
        wx.navigateTo({
          url: '../files/files',
        })
    },

    getOpenid: function() {
        wx.cloud.callFunction({
            name: 'getOpenid'
        }).then(res => {
            console.log(res)
        }).catch(err => {
            console.log(err)
        });
    },

    sum: function(event) {
        wx.cloud.callFunction({
            name: 'sum',
            data: {
                a: 4,
                b: 5
            }
        }).then(res => {
            console.log(res)
        })
    },

    cloudFunInsert: function() {
        wx.cloud.callFunction({
            name: 'operateDB',
            data: {
                type: 'insert',
                name: 'zhao',
                age: 40
            }
        }).then(res => {
            console.log(res)
        })
    },

    cloudFunUpdate: function() {
        wx.cloud.callFunction({
            name: 'operateDB',
            data: {
                type: 'update',
                name: 'zhao',
                age: 26
            }
        }).then(res => {
            console.log(res)
        })
    },

    cloudFunQuery: function() {
        wx.cloud.callFunction({
            name: 'operateDB',
            data: {
                type: 'query',
                age: 26
            }
        }).then(res => {
            console.log(res)
        })
    },

    cloudFunDelete: function() {
        wx.cloud.callFunction({
            name: 'operateDB',
            data: {
                type: 'delete',
                age: 26
            }
        }).then(res => {
            console.log(res)
        })
    }

})